﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ucFilesOper
{
    public partial class TrimFileName : DevExpress.XtraEditors.XtraUserControl
    {
        private  static readonly TrimFileName _oneInstance = new TrimFileName();
        public static TrimFileName OneInstance
        {
            get { return _oneInstance; }
        }
        private string _currentFilePath = "";
        private string _currentDirPath = "";
        private string []  _filesPath  = null;
        private bool _isIncludeChildDir = false;
        public string ParentalDir
        {
            get
            {
                if (string.IsNullOrEmpty(_currentFilePath))
                {
                    return _currentDirPath;
                }
                else
                {
                    return Path.GetDirectoryName(_currentFilePath);
                }
            }
        }
        public string FileExtension
        {
            get { return Path.GetExtension(_currentFilePath); }
        }

        private TrimFileName()
        {
            InitializeComponent();
        }
#region 界面响应

        private void BtnOpenFile_Click(object sender, EventArgs e)
        {
            FileDialog open = new OpenFileDialog();
            open.InitialDirectory = @"f:\";
            open.RestoreDirectory = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                _currentFilePath = open.FileName;
                txeFileName.Text = _currentFilePath;
                ShowFiles(Path.GetDirectoryName(txeFileName.Text));
            }
        }

        private void BtnOpenDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog open = new FolderBrowserDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                txeDirName.Text = open.SelectedPath;
                _currentDirPath = open.SelectedPath;
                ShowFiles(txeDirName.Text);
            }
        }

#endregion

        #region 私有函数
        private void ShowFiles(string path)
        {
            try
            {
                this._filesPath = Directory.GetFiles(path, @"*.*",
                                                    this._isIncludeChildDir
                                                         ? SearchOption.AllDirectories
                                                         : SearchOption.TopDirectoryOnly);
            }
            catch (Exception)
            {
            }
            lstFiles.Items.Clear();
            foreach (string str in this._filesPath)
            {
                lstFiles.Items.Add(str);
            }

        }

        #endregion

        /// <summary>
        /// 执行改名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDoTrim_Click(object sender, EventArgs e)
        {
            string rexStr = txeTrimStr.Text;
            foreach (string str in this._filesPath)
            {
                string tempFileName = Path.GetFileName(str);
                Regex regex = new Regex(rexStr);
                if (tempFileName.IndexOf(txeTrimStr.Text, System.StringComparison.Ordinal)>-1)
                {
                     string fileNewName = Regex.Replace(tempFileName, rexStr,"");
                    FileInfo tempFile = new FileInfo(Path.Combine(ParentalDir, tempFileName));
                  //  tempFile.MoveTo(Path.Combine(ParentalDir, fileNewName));
                   lstNewFiles.Items.Add( ChangeFileName(tempFile, fileNewName));
                }
            }
            ShowFiles(ParentalDir);
        }

        #region 私有函数
        private string ChangeFileName(FileInfo tempFile, string fileNewName)
        {
            string fileNewFullName = Path.Combine(tempFile.DirectoryName, fileNewName);
            int i = 1;
            while (File.Exists(fileNewFullName))
            {
                string s1 = Path.GetDirectoryName(fileNewFullName);
                string s2 = Path.GetFileNameWithoutExtension(fileNewFullName);
                string s3 = Path.GetExtension(fileNewFullName);
                fileNewFullName = s1 + @"\" + s2 + i + "." + s3;
                i++;
            }
            try
            {
                tempFile.MoveTo(fileNewFullName);
            }
            catch (Exception)
            {
                return "出错了";
            }
            return Path.GetFileName(fileNewFullName);
        }
        #endregion


    }
}
